﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Tour.Linq.TourDBML;
public partial class AdminManager_Admin_TypeManager_Add : AdminBasePage
{

    protected int _ParentID;

    protected int _TypeID;

    protected override void OnInit(EventArgs e)
    {
        if (Request["ParentID"] != null) _ParentID = Request["ParentID"].ToInt();
        if (Request["TypeID"] != null) _TypeID = Request["TypeID"].ToInt();
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DisplayInfo();
        }
    }

    private void DisplayInfo()
    {
        BindParentTree();
        if (_TypeID > 0)
        {
            TourType tourType = TourContext.TourType.First(p => p.ID == _TypeID);
            txtTypeName.Text = tourType.TypeName;
            ddlParentID.SelectedValue = tourType.ParentID.ToString();
            LinkButton1.Text = "修改分类";
        }
    }

    private void BindParentTree()
    {
        if (!Page.IsPostBack)
        {
            ddlParentID.DataTextField = "TypeName";
            ddlParentID.DataValueField = "ID";
            ddlParentID.DataSource = TourStaticData.TreeTourType;
            ddlParentID.DataBind();
            ddlParentID.Items.Add(new ListItem("主分类", "0"));
            ddlParentID.SelectedValue = _ParentID.ToString();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (CheckEnterInfo())
        {
            _ReturnUrl = "Admin_TypeManager.aspx";
            TourType tourType;
            if (_TypeID > 0)
            {
                tourType = TourContext.TourType.First(p => p.ID == _TypeID);
                tourType.TypeName = txtTypeName.Text;
                tourType.ParentID = ddlParentID.SelectedValue.ToInt();
                _Message = "更新类别成功";

            }
            else
            {
                tourType = new TourType();
                tourType.ParentID = ddlParentID.SelectedValue.ToInt();
                tourType.TypeName = txtTypeName.Text;
                TourContext.TourType.InsertOnSubmit(tourType);
                _Message = "添加新分类成功";
            }
            TourContext.SubmitChanges();
            TourStaticData.Clean();
        }
    }
}
